package com.guanganren.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.guanganren.db.entity.Area;
import com.guanganren.db.entity.Catg;
import com.guanganren.service.AreaService;
import com.guanganren.service.CatgService;
import com.opensymphony.xwork2.ActionSupport;

public class CatgAction extends ActionSupport{

	private CatgService catgService;
	private AreaService areaService;
	private Catg catg = new Catg();
	private List<Catg> catgListForRelease;
	public CatgService getCatgService() {
		return catgService;
	}

	public void setCatgService(CatgService catgService) {
		this.catgService = catgService;
	}
	
	public AreaService getAreaService() {
		return areaService;
	}

	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}

	public Catg getCatg() {
		return catg;
	}

	public void setCatg(Catg catg) {
		this.catg = catg;
	}

	public List<Catg> getCatgListForRelease() {
		return catgListForRelease;
	}

	public void setCatgListForRelease(List<Catg> catgListForRelease) {
		this.catgListForRelease = catgListForRelease;
	}

	public String execute(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		String catgId = request.getParameter("id");
		List<Catg> catgList = null;
		List<Area> areaList = null;
		try{
			areaList = areaService.getAreaByParentId("0");
			Catg catgTemp = catgService.getCatgById(catgId);
			String parentId=null;
			if(catgTemp!=null){
				parentId = catgTemp.getCatg().getCatgId().toString();
			}
			if(parentId!=null){
			if(parentId.equals("0")){
				catgList = catgService.getCatgByParentId(catgId);
				this.catg.setCatgId(new Integer(catgId));
				this.catg.setCatgName(catgTemp.getCatgName());
			}else{
				catgList = catgService.getCatgByParentId(parentId);
				this.catg.setCatgId(new Integer(parentId));
				this.catg.setCatgName(catgTemp.getCatgName());
			}
			}
			session.setAttribute("areaList",areaList);
			session.setAttribute("catgList",catgList);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return "list";
	}
	public String release(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String catgId = request.getParameter("cid");
		try {
			catgListForRelease = catgService.getCatgByParentId(catgId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "release";
	}
}
